<?php

namespace App\Models\Entity\Tag;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class tag
 * @package App\Models\Entity\Tag
 * @version June 20, 2018, 4:23 pm CST
 *
 * @property string name
 * @property string tag_group
 * @property string description
 * @property integer listorder
 */
class tag extends Model
{
    use SoftDeletes;

    public $table = 'tags';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'tag_group',
        'description',
        'listorder',
        // icon值
        'img',
        // icon类型
        'icon_type',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name'        => 'string',
        'tag_group'   => 'integer',
        'description' => 'string',
        'listorder'   => 'integer',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name'      => 'required|max:20',
        'tag_group' => 'required',
        'listorder' => 'required',
    ];

    public function tagGroup()
    {
        return $this->belongsTo(\App\Models\Entity\sys_categories\sys_categories::class, 'tag_group');
    }

    /**
     * 获得此标签下所有的点播媒资。
     */
    public function vasseti()
    {
        return $this->morphedByMany('\App\Models\Entity\Asset\Video\Vasseti', 'tag_content');
    }

    /**
     *  获得此标签下所有的点播媒资。
     */
    public function vchan()
    {
        return $this->morphedByMany('\App\Models\Entity\Asset\Vlive\Vchan', 'tag_content');
    }

    /**
     *  获得此标签下所有的音频媒资。
     */
    public function iaasset()
    {
        return $this->morphedByMany('\App\Models\Entity\Asset\Iaasset', 'tag_content');
    }

    /**
     *  获得此标签下所有的音频媒资。
     */
    public function pic_albun()
    {
        return $this->morphedByMany('\App\Models\Entity\pic_albums\pic_albums', 'tag_content');
    }

    /**
     * 获取此标签下所有栏目列表
     */
    public function collist()
    {
        return $this->morphedByMany('\App\Models\Entity\Column\Collist', 'tag_content')->withTimestamps();
    }
    /**
     * 获取此标签下所有地栏目
     * @return [type] [description]
     */
    public function muticolumn()
    {
        return $this->morphedByMany('\App\Models\Application\Muticolumn', 'tag_content')->withTimestamps();
    }

    /**
     * 获取此标签下所有地栏目列表数据
     * @return [type] [description]
     */
    public function collistData()
    {
        return $this->morphedByMany('\App\Models\Application\CollistData', 'tag_content')->withTimestamps();
    }

    /**
     * 获取此标签下所有的页面推荐位数据
     * @return [type] [description]
     */
    public function apppageRectplData()
    {
        return $this->morphedByMany('\App\Models\Recommend\ApppageRectplData', 'tag_content')->withTimestamps();
    }
    public function tagList($where, $start, $length, $ord)
    {
        $recordsTotal    = $this->where($where)->count();
        $recordsFiltered = $recordsTotal;
        $data            = $this->with(['tagGroup'])->where($where)
            ->select()
            ->orderByRaw($ord)
            ->skip($start)->take($length)
            ->get();
        return array($recordsTotal, $recordsFiltered, $data);
    }

}
